package com.djhu.msg.annotation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ClassUtils;

import javax.jms.Message;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

/**
 * Created by zw on 2018/3/16.
 */
public class MsgContainer implements ApplicationContextAware,InitializingBean{

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, Object> beanMap = applicationContext.getBeansWithAnnotation(Listener.class);
        for (Map.Entry<String, Object> entry:beanMap.entrySet()){

            Method[] methods = findMethods(entry.getValue());
            for (Method method:methods){
                //BeanUtils.
                if (method.isAnnotationPresent(Listener.class) &&  method.getParameterTypes().length == 1){
                    Class<?>[] classes = method.getParameterTypes();
                    //method.
                    Set<Class<?>> set = ClassUtils.getAllInterfacesForClassAsSet(classes[0]);
                    if (set.contains(Message.class)){
                        Method m = method;
                        Object bean = entry.getValue();

                    }
                    classes[0].getInterfaces();
                    //BeanUtils
                    //method.getParameterTypes().length != 1
                }
                method.isAnnotationPresent(Listener.class);


            }
            //entry.getValue();
            //BeanUtils.
        }
    }


    public Method[] findMethods(Object object) {
        return object.getClass().getMethods();
    }
}